#include <stdlib.h>

#include "interactivTable.h"

interactivTable::interactivTable() 
{
	row_count = 10;

	cWeather help_Row;
	for (int i = 1; i<= row_count; i++) {
		
		help_Row.id=i;
		char buffer[50];
		sprintf(buffer, "City %d:", i );
		help_Row.city.assign(buffer);
		
		int RANGE_MIN = 0;
		int RANGE_MAX = 5;
		int range = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
		help_Row.today=20+range;
		range = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
		help_Row.tomorrow=20+range;
		range = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
		help_Row.day_after_tomorrow=20+range;
		
		WeatherTable.push_back(help_Row);
	}
	
}


interactivTable::~interactivTable() 
{
	WeatherTable.clear();
}

net_fnc_s opcd interactivTable::accept_connect()
{	
	return 0;
}

net_fnc opcd interactivTable::get_table( net_out std::vector< cWeather > & weather_table )
{
	
	weather_table=WeatherTable;
	return 0;
}

//get only changed rows
net_fnc opcd interactivTable::get_change( net_out std::vector< cWeather >  & changed_rows )
{
	changed_rows.clear();
	cWeather hw;
	if (WeatherTable.size() != 0) {
		std::vector< cWeather >::iterator W_iter;
		W_iter = WeatherTable.begin();
		hw= *W_iter;
		for (int i = 1; i<= row_count; i++ ) {
			hw = *W_iter;
			
			int RANGE_MIN = 0;
			int RANGE_MAX = 2 ;
			int range1 = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);	
			if (range1 == 1) {		//change temerature of this city
				for (int j = 1; j<=3; j++) {
					int range2 = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
					if ( range2 == 1 ) {
						RANGE_MIN = 0;
						RANGE_MAX = 10 ;
						int range3 = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
						hw.today=20+range3;
				        range3 = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
						hw.tomorrow=20+range3;
				        range3 = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
						hw.day_after_tomorrow=20+range3;
					}
				}
				changed_rows.push_back(hw);
			}
			W_iter++;
		}
		if (changed_rows.size() == 0 ) {
			hw.id=0;
			char buffer[50];
			sprintf(buffer, "City 0" );
			hw.city.assign(buffer);
			hw.day_after_tomorrow=0;
			hw.today=0;
			hw.tomorrow=0;
			changed_rows.push_back(hw);
		}
	} else {
		hw.id=0;
		char buffer[50];
		sprintf(buffer, "City 0" );
		hw.city.assign(buffer);
		hw.day_after_tomorrow=0;
		hw.today=0;
		hw.tomorrow=0;
		changed_rows.push_back(hw);
	}
	return 0;
}